package product.merchant;

import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.weixin.WebUser;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import play.db.jpa.JPA;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * 商品商户关系
 * Created by buhaoba on 16/6/16.
 */
@Entity
@Table(name = "product_spec_merchant_price_group_webusers")
public class ProductSpecMerchantPriceGroupWebUser extends BaseModel {


    /**
     * 产品价格组
     */
    @JoinColumn(name = "price_group_id")
    @ManyToOne
    public ProductSpecMerchantPriceGroup priceGroup;

    /**
     * 会员
     */
    @JoinColumn(name = "web_user_id")
    @ManyToOne
    public WebUser webUser;


    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark ;


    /**
     *
     * @param groupId
     * @return
     */
    public static List<ProductSpecMerchantPriceGroupWebUser> loadByGroup(long groupId){
        return ProductSpecMerchantPriceGroupWebUser.find(" priceGroup .id = ? and deleted = ?" , groupId , DeletedStatus.UN_DELETED).fetch();
    }

    /**
     *
     * @param specMerchantId
     * @param webUser
     * @return
     */
    public static ProductSpecMerchantPriceGroupWebUser findBySpecMerchantIdAndWebUser(Long specMerchantId , WebUser webUser) {
        return ProductSpecMerchantPriceGroupWebUser.find("priceGroup.productSpecMerchant.id = ? and webUser = ? and deleted = ?" , specMerchantId , webUser , DeletedStatus.UN_DELETED).first();
    }


    /**
     * 最小值
     * @param productId
     * @param webUser
     * @return
     */
    public static Double findMinBySpecMerchantIdAndWebUser(Long productId , WebUser webUser , Merchant merchant) {
        ProductSpecMerchant productSpecMerchant = ProductSpecMerchant.findMinPriceByProduct(productId , merchant);
        ProductSpecMerchantPriceGroupWebUser priceGroupWebUser = findBySpecMerchantIdAndWebUser(productSpecMerchant.id , webUser);
        if(priceGroupWebUser == null) {
            return productSpecMerchant.merchantPrice;
        }
        return priceGroupWebUser.priceGroup.price;
    }

    /**
     * 最大值
     * @param productId
     * @param webUser
     * @return
     */
    public static Double findMaxBySpecMerchantIdAndWebUser(Long productId , WebUser webUser , Merchant merchant) {
        ProductSpecMerchant productSpecMerchant = ProductSpecMerchant.findMaxPriceByProduct(productId , merchant);
        ProductSpecMerchantPriceGroupWebUser priceGroupWebUser = findBySpecMerchantIdAndWebUser(productSpecMerchant.id , webUser);
        if(priceGroupWebUser == null) {
            return productSpecMerchant.merchantPrice;
        }
        return priceGroupWebUser.priceGroup.price;
    }

    /**
     *
     * @param delIds
     */
    public static void deleteWebUser(Integer[] delIds){
        String str = UStringUtil.concatStr(",",delIds);
        String sql = " update product_spec_merchant_price_group_webusers a set a.deleted = 1 where a.id in ("+str+")";
        JPA.em().createNativeQuery(sql).executeUpdate();
    }

    /**
     *
     * @param groupId
     */
    public static void deleteWebUserByGroup(long groupId){
        String sql = " update product_spec_merchant_price_group_webusers a set a.deleted = 1 where a.price_group_id = "+groupId;
        JPA.em().createNativeQuery(sql).executeUpdate();
    }


    //根据,wenUser , productSpecMerchant , 查询商品优惠价格
    public static Double getCouponPrice(long webUserId , long specMerchantId){

        ProductSpecMerchantPriceGroupWebUser groupWebUser =ProductSpecMerchantPriceGroupWebUser.find("webUser.id = ? and priceGroup.productSpecMerchant.id = ?  and deleted = ? and priceGroup.deleted = ?" ,webUserId , specMerchantId , DeletedStatus.UN_DELETED , DeletedStatus.UN_DELETED ).first();
        if(groupWebUser != null && groupWebUser.priceGroup != null && groupWebUser.priceGroup.price != null)
            return groupWebUser.priceGroup.price ;
        else
            return null;
    }

    public static List<Map<String , Object>> getCouponPriceByList(long webUserId ,List<Long> ids) {
        String idStr = UStringUtil.concatStr(",",ids);
        String sql = " select ppg.price,ppg.product_spec_merchant_id as id from product_spec_merchant_price_group_webusers pg LEFT JOIN product_spec_merchant_price_groups ppg on pg.price_group_id = ppg.id where ppg.product_spec_merchant_id in ("+idStr+") and pg.web_user_id = " +webUserId +" and pg.deleted = 0 and ppg.deleted = 0 and ppg.price is not null" ;
        Query query = JPA.em().createNativeQuery(sql);
        query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String , Object>> resultList = query.getResultList();
        return resultList;
    }

}
